<?php
class Reale_Model_Estate_Property extends Reale_Model_Estate_Generic
{
	/**
	 * Stores owner address
	 * @var string
	 */
	protected $_owner_address;
	
	/**
	 * Stores property's name
	 * @var string
	 */
	protected $_property_name;
	
	/**
	 * Stores AGENT_ID if property refered by agent.
	 * @var string
	 */
	protected $_referer;
	
	/**
	 * Alternate number 
	 * @var int Alternate number.
	 */
	protected $_alternate_number;
	
	/**
	 * Property Specification
	 * @var Reale_Model_Estate_Spec
	 */
	protected $_spec;
	
	
	/**
	 * Stores entity mapper
	 * @var Reale_Model_Estate_PropertyMapper
	 * 
	 */
	protected $_mapper;
	

    /**
     * Constructor
     * 
     * @param  array|null $options 
     * @return void
     */
    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
            $this->getSpec()->setOptions($options);
        }
    }
	
    /**
     * Overloading: allow property access
     * 
     * @param  string $name 
     * @param  mixed $value 
     * @return void
     */
    public function __set($name, $value)
    {
        $method = 'set' . $name;
        if ('mapper' == $name || !method_exists($this, $method)) {
            throw new Exception('Invalid property specified',Zend_Log::DEBUG);
        }
        $this->$method($value);
    }

    /**
     * Overloading: allow property access
     * 
     * @param  string $name 
     * @return mixed
     */
    public function __get($name)
    {
        $method = 'get' . $name;
        if ('mapper' == $name || !method_exists($this, $method)) {
             throw new Exception('Invalid property specified',Zend_Log::DEBUG);
        }
        return $this->$method();
    }
    
    /**
     * Set object state
     * 
     * @param  array $options 
     * @return Reale_Model_Estate_Property
     */
    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }
    
	/**
     * @return string $_owner_address
     */
    public function getOwner_address ()
    {
        return $this->_owner_address;
    }

	/**
     * @param string $_owner_address
     * @return Reale_Model_Estate_Property
     */
    public function setOwner_address ($_owner_address)
    {
        $this->_owner_address = $_owner_address;
        return $this;
    }

	/**
     * @return string $_property_name
     */
    public function getProperty_name ()
    {
        return $this->_property_name;
    }

	/**
     * @param string $_property_name
     * @return Reale_Model_Estate_Property
     */
    public function setProperty_name ($_property_name)
    {
        $this->_property_name = $_property_name;
        return $this;
    }

	/**
     * @return string $_referer
     */
    public function getReferer ()
    {
        return $this->_referer;
    }

	/**
     * @param string $_referer
     * @return Reale_Model_Estate_Property
     */
    public function setReferer ($_referer)
    {
        $this->_referer = $_referer;
        return $this;
    }

	/**
     * @return the $_alternate_number
     */
    public function getAlternate_number ()
    {
        return $this->_alternate_number;
    }

	/**
     * @param int $_alternate_number
     */
    public function setAlternate_number ($_alternate_number)
    {
        $this->_alternate_number = $_alternate_number;
    }

	/**
     * @return Reale_Model_Estate_Spec $_spec
     */
    public function getSpec ()
    {
        if (null === $this->_spec) {
            $this->setSpec(new Reale_Model_Estate_Spec());
        }
        return $this->_spec;
    }

	/**
     * @param Reale_Model_Estate_Spec $_spec
     * @return Reale_Model_Estate_Property
     */
    public function setSpec (Reale_Model_Estate_Spec $_spec)
    {
        $this->_spec = $_spec;
        return $this;
    }

	/**
	 * Setter function for property mapper 
     * @return Reale_Model_Estate_Property
	 */
	public function setMapper($mapper) {
		$this->_mapper = $mapper;
		return $this;
	}
	
	/**
	 * Getter function for property mapper
	 * @return Reale_Model_Estate_PropertyMapper $_mapper
	 */
	public function getMapper(){
        if (null === $this->_mapper) {
            $this->setMapper(new Reale_Model_Estate_PropertyMapper());
        }
        return $this->_mapper;
    }
    
    
    /**
     * Set object to array
     * 
     * @return array
     */
    public function __toArray()
    {
        $data = array();
        $methods = get_class_methods($this);
        foreach ($methods as $key => $method) {
            if ('get' == substr($method, 0,3)) {
                $output = $this->$method();
                if (is_array($output) || is_string($output)) {
                    $newKey = strtolower(substr($method, 3));
                    $data[$newKey] = $output;
                }
            };
        }
        return $data;
    }
    
    /**
     * To save new or update existing property's informtion.
     * return Reale_Model_Estate_Property | null
     * 
     */
    public function save() {
    	$return = $this->getMapper()->save($this);
    	if ($return) {
    	    /* I think not need as mapper accepts &$this
        	if ($return instanceof Reale_Model_Estate_Property) {
        		$this->setEstate_id($return->getEstate_id());
        	}*/
        	return true;
    	}
    	return null;
    } 

	/**
	 * 
	 * Getter function for data member estate_type
	 * @return string $_estate_type
	 */
	public function getEstate_Type() {
		if(!isset($this->_estate_type)){
		    self::setEstate_Type('PROPERTY');
		}
		return $this->_estate_type;	
		//throw new Exception('The objects primary key(i.e. $_estate_type) is not set.',Zend_Log::ERR); 
	}
	
	public function fetch(){
		return $this->getMapper()->fetch($this);
	}
}

